package com.rdxer.service;

import com.rdxer.api.para.SaveAccountPara;
import com.rdxer.core.ex.ListEx;
import com.rdxer.core.security.CheckService;
import com.rdxer.core.utils.IdWorker;
import com.rdxer.core.utils.SearchUtils;
import com.rdxer.core.utils.SuperBeanUtils;
import com.rdxer.core.utils.search.SearchPara;
import com.rdxer.dataview.AccountDV;
import com.rdxer.model.Account;
import com.rdxer.repository.AccountRepository;
import lombok.Getter;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
@Log4j2
public class AccountManagerService {
    @Getter
    @Autowired
    AccountRepository repository;

    @Autowired
    PasswordEncoder passwordEncoder;
    @Autowired
    IdWorker idWorker;

    @Autowired
    AccountService accountService;



    /**
     * 条件查询+分页
     */
    public Page<Account> search(SearchPara para) {
        return SearchUtils.searchByPage(repository, para);
    }

    public Page<AccountDV> accountPageToDVPage(Page<Account> data) {

        List<Account> content = data.getContent();

        List<AccountDV> dataDV = new ArrayList<>();

        for (Account account : content) {
            var adv = new AccountDV();
            BeanUtils.copyProperties(account, adv);
            adv.setRoles(account.getRoles().split(","));
            dataDV.add(adv);
        }

        Page<AccountDV> result = new PageImpl<AccountDV>(dataDV, data.getPageable(), data.getTotalElements());

        return result;
    }


    public void disable(String id) {
        Account byId = repository.findById(id).get();
        byId.setDisabledDate(System.currentTimeMillis());
        repository.save(byId);
    }

    public void enable(String id) {
        Account byId = repository.findById(id).get();
        byId.setDisabledDate(null);
        repository.save(byId);
    }

    @Autowired
    CheckService checkService;

    public void enableAllByCheckPerms(List<String> ids) {
        for (String id : ids) {
                enable(id);
        }
    }

    public void disableAllByCheckPerms(List<String> ids) {
        for (String id : ids) {
                disable(id);
        }
    }

    // 管理端 创建用户  +   更新用户
    public void save(SaveAccountPara para) {

        Account account;
        account = new Account();
        SuperBeanUtils.copyNotNullProperties(para, account);
        account.setRoles(ListEx.join(para.getRoles(),","));
        // 基本信息保存
        if (para.getId() == null) {
            // 待创建
            account = accountService.register(account);

        } else {
            Account curr = accountService.show(para.getId());
            accountService.checkByUpdate(curr, account);
            if (!StringUtils.isEmpty(para.getPassword())) {
                String encodepwd = passwordEncoder.encode(para.getPassword());
                account.setPassword(encodepwd);
            }
            account = accountService.updateOfPatch(para.getId(), account);
        }
    }

    public Account findById(String id) {
        if (id == null) {
            return null;
        }
        Optional<Account> byId = getRepository().findById(id);
        if (byId.isEmpty()){
            return  null;
        }
        return byId.get();
    }
}
